package org.ecloud.base.identifier;

import java.io.Serializable;

import org.ecloud.base.identifier.config.IdConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DefaultIdGenerator implements IdGenerator, Serializable {

	/**
	 * serialVersionUID:
	 */
	private static final long serialVersionUID = 3405824291434808429L;
	private IdWorker idWorker;
	@Autowired
	private IdConfig config;
	
	@Override
	public String getId() {
		if (idWorker == null)
			init();
		return getUId().toString();
	}

	@Override
	public Long getUId() {
		if (idWorker == null)
			init();
		return idWorker.nextId();
	}

	private void init() {
		idWorker = new IdWorker(config.getWorkerId(), config.getDatacenterId());
	}
}
